#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> ret;
        sort(nums.begin(), nums.end());
        int i = 0;
        while (i < nums.size())
        {
            int left = i + 1;
            int right = nums.size() - 1;
            while (left < right)
            {
                if (nums[left] + nums[right] > 0 - nums[i])
                {
                    right--;
                }
                else if (nums[left] + nums[right] < 0 - nums[i])
                {
                    left++;
                }
                else {
                    ret.push_back({ nums[i],nums[left],nums[right] });
                    left++;
                    right--;
                    while (nums[left - 1] == nums[left] && left < right)
                    {
                        left++;
                    }
                    while (nums[right + 1] == nums[right] && left < right)
                    {
                        right--;
                    }
                }

            }
            i++;
            while (i < nums.size() && nums[i - 1] == nums[i])
            {
                i++;
            }
        }
        return ret;
    }
};
int main()
{
    Solution as;
    vector<int> df = { 12,34,-4,543,-6,57,56,-6,787,867,8,769,789,-30 };
    vector<vector<int>> aq = as.threeSum(df);
    for (auto& e : aq)
    {
        for (auto& d : e)
        {
            cout << d << " ";
        }
        cout << endl;

    }

    return 0;
}